package com.booza.model;

import java.io.Serializable;
import java.sql.Time;
import java.util.Calendar;
import java.util.Date;

import com.booza.constants.TournamentStatus;

/**
 * Domain model of system generated tournaments. Because system generated tournaments run regularly, normally every day
 * we need this tournament definition to define the tournament and a tournament instance for the specific daily instance
 * of the tournament @see TournamentInstance.
 */
public class TournamentDef extends TournamentBase implements Serializable
{
	private static final long serialVersionUID = -1L;
	
	private int tournamentid;
	private int maxplayers;
	private int round0;
	private int round1;
	private int round2;
	private int round3;
	private Time starttime;
	private int occurrence;
	private int gamekind;
	private String description;
	private String name;
	private int timelimit;
	private int wager = 0;
	private TournamentInstance instance;
	private String winner;
	private GameType game;

	public GameType getGame()
	{
		return game;
	}

	public void setGame(GameType game)
	{
		this.game = game;
	}

	public String getWinner()
	{
		return winner;
	}

	public void setWinner(String winner)
	{
		this.winner = winner;
	}

	public TournamentInstance getInstance()
	{
		return instance;
	}

	public int getShottimelimit()
	{
		return this.timelimit;
	}

	public void setInstance(TournamentInstance instance)
	{
		this.instance = instance;
	}

	public int getWager()
	{
		return wager;
	}

	public void setWager(int wager)
	{
		this.wager = wager;
	}

	public int getTimelimit()
	{
		return timelimit;
	}

	public void setTimelimit(int timelimit)
	{
		this.timelimit = timelimit;
	}

	public String getDescription()
	{
		return description;
	}

	public void setDescription(String description)
	{
		this.description = description;
	}

	public int getGamekind()
	{
		return gamekind;
	}

	public void setGamekind(int gamekind)
	{
		this.gamekind = gamekind;
	}

	public int getMaxplayers()
	{
		return maxplayers;
	}

	public void setMaxplayers(int maxplayers)
	{
		this.maxplayers = maxplayers;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public int getOccurrence()
	{
		return occurrence;
	}

	public void setOccurrence(int occurrences)
	{
		this.occurrence = occurrences;
	}

	public int getRound0()
	{
		return round0;
	}

	public void setRound0(int round0)
	{
		this.round0 = round0;
	}

	public int getRound1()
	{
		return round1;
	}

	public void setRound1(int round1)
	{
		this.round1 = round1;
	}

	public int getRound2()
	{
		return round2;
	}

	public void setRound2(int round2)
	{
		this.round2 = round2;
	}

	public int getRound3()
	{
		return round3;
	}

	public void setRound3(int round3)
	{
		this.round3 = round3;
	}

	public Time getStarttime()
	{
		return starttime;
	}

	public void setStarttime(Time starttime)
	{
		this.starttime = starttime;
	}

	public int getTournamentid()
	{
		return tournamentid;
	}

	public void setTournamentid(int tournamentid)
	{
		this.tournamentid = tournamentid;
	}

	public Date getTournamentCompletionTime()
	{
		return this.getInstance().getEndtime();
	}

	public boolean isStartingTomorrow()
	{
		Calendar c = Calendar.getInstance();
		c.set(Calendar.HOUR_OF_DAY, starttime.getHours());
		c.set(Calendar.MINUTE, starttime.getMinutes());
		c.set(Calendar.SECOND, starttime.getSeconds());
		return Calendar.getInstance().after(c);
	}
	
	public Date getStartDate()
	{
		// create calendar and set time
		Calendar c = Calendar.getInstance();
		c.set(Calendar.HOUR_OF_DAY, starttime.getHours());
		c.set(Calendar.MINUTE, starttime.getMinutes());
		c.set(Calendar.SECOND, starttime.getSeconds());
		
		// if tournament is happening tomorrow add 1 day to start date		
		if(Calendar.getInstance().after(c))
		{
			c.add(Calendar.DATE, 1);
		}		
		return c.getTime();
	}

	public int getStatus()
	{
		return this.instance != null ? this.instance.getStatus() : TournamentStatus.NOT_STARTED;
	}
}
